// #include "main.h"
#include <zephyr/kernel.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/services/nus.h>
#include <zephyr/bluetooth/hci.h>

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
bool bt_notify_enabled = false;
bool bt_connected = false;

static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};

static const struct bt_data sd[] = {
    BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_SRV_VAL),
};

static void notif_enabled(bool enabled, void *ctx)
{
    ARG_UNUSED(ctx);

    printk("%s() - %s\n", __func__, (enabled ? "Enabled" : "Disabled"));

    bt_notify_enabled = enabled;
}

static void received(struct bt_conn *conn, const void *data, uint16_t len, void *ctx)
{
    ARG_UNUSED(conn);
    ARG_UNUSED(ctx);

    printk("%s() - Len: %d, Message: %.*s\n", __func__, len, len, (char *)data);
}

struct bt_nus_cb nus_listener = {
    .notif_enabled = notif_enabled,
    .received = received,
};
static void connected(struct bt_conn *conn, uint8_t err)
{
    if (err)
    {
        printk("ay  Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err));
    }
    else
    {
        printk("Connected\n");
        bt_connected = true;

        //    bt_le_adv_stop();
        // bt_nus_cb_register
    }
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
    printk("ay Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));
    int err;

    bt_connected = false;
    //  err =bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
}
void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
{
    printk("Updated MTU: TX: %d RX: %d bytes\n", tx, rx);
}
static void restart_adv(void)
{
    int err;
    printk("重新广播\n");

    err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    if (err)
    {
        printk("Advertising failed to start (err %d)\n", err);
    }
    else
    {
        printk("Advertising successfully started\n");
    }
}
static struct bt_gatt_cb gatt_callbacks = {
    .att_mtu_updated = mtu_updated};

BT_CONN_CB_DEFINE(conn_callbacks) = {
    .connected = connected,
    .disconnected = disconnected,
    .recycled = restart_adv,

};

int ble_init(void)
{
    int err;

    printk("开始打开程序了\n");

    err = bt_nus_cb_register(&nus_listener, NULL);
    if (err)
    {
        printk("Failed to register NUS callback: %d\n", err);
        return err;
    }
    // 启动蓝牙协议栈
    err = bt_enable(NULL);
    if (err)
    {
        printk("Failed to enable bluetooth: %d\n", err);
        return err;
    }

    if (bt_is_ready())
    {
        printk("蓝牙OK\n");
    }
    else
    {
        printk("蓝牙没OK===============================\n");
        return -1;
    }

    try
    {
        err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
        if (err)
        {
            printk("Failed to start advertising: %d\n", err);
            return err;
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    printk("Initialization complete\n");

    bt_gatt_cb_register(&gatt_callbacks);
    k_sleep(K_MSEC(1000));
}
int tx_count_ble = 1;
void ble_send_data(const char *str)
{

    int err;

    if (!bt_connected)
    {

        return;
    }
    if (!bt_notify_enabled)
    {

        printk("你TM 连接了 没订阅呢\n");

        return;
    }
    int len = strlen(str);

    uint8_t *ptr8 = (uint8_t *)str;
    int tmp_count = 0;
    while (len > 0)
    {
        tmp_count++;

        if (len > 500)
        {
            err = bt_nus_send(NULL, ptr8, 500);
            ptr8 += 500;
        }
        else
        {
            err = bt_nus_send(NULL, ptr8, len);
        }

        len -= 500;
        printk("发送批次:%d\n", tmp_count);
    }

    printk("发送了:%d次哦\n", tx_count_ble++);
}

void ble_send_data_hex(uint8_t *data)
{
    int err;

    if (!bt_connected)
    {

        return;
    }
    if (!bt_notify_enabled)
    {

        printk("你TM 连接了 没订阅呢\n");

        return;
    }

    for (int i = 0; i < 8; i++)
    {

        err = bt_nus_send(NULL, data + (i * 512), 512);
        printk("发送批次:%d\n", i + 1);
    }
}